// "Add to basket" button
(function(jQuery) { // remove product from basket SKELETON BAHEVIOUR
	   jQuery.fn.addProductToBasket = function(settings) {

		var config = {
			'notify': function(messageBox, data){messageBox.html('<span>'+data.message+'</span>');},
			'postAdd': function(data){jQuery('#basket_preview_box').html(data.html)},
			'preAdd' : function(){},
                        'loading': 'loading'
		};
		 settings = jQuery.extend(config, settings);

		 var notify = settings['notify'];
		 var postAdd = settings['postAdd'];
		 var preAdd = settings['preAdd'];
                 


	     this.each(function(i, elm) {
	    	jQuery(elm).click(function(e){
				e.preventDefault();
				var link = jQuery(this);
                                var container = link.parents('.item');
                                var href = container.find('input:radio:checked').attr('data-url');
				
                                if(!href){
                                    return false;
                                }
                                
                                preAdd();
				
                                
				var par = link.parent();
                                
				var qty = jQuery('input[name=qty]',par).val();

                                container.mask(config.loading);
				jQuery.post(href,{qty: qty}, function(data){
					notify(link, data);
					postAdd(data);
                                        container.unmask();
                                        //jQuery('#gotobasket').show();

				},'json');
				
				return false;
	    	});

	     });

	     return this;
	   };
})(jQuery);


jQuery(document).ready(function(){
	jQuery('.minus, .pluss').live('click',function(e){ // "-" or "+" CLICKED -> change value -> trigger input CHANGED event
		e.preventDefault();
		jQuery('#basketList').mask("Arvutan...");
		var input = jQuery(this).parent('.qty').find('.qtyinput');
		var val = parseInt(input.val());
		var incdec = jQuery(this).hasClass('minus') ? -1 : 1;

		if(val + incdec <= 0) return false;
//		if(val + incdec > input.attr('alt')){
//			alert(jQuery('#notinstock').text());
//			return false;
//		}
		input.val(val + incdec);
		input.trigger('change');

		return true;
	});

//	jQuery('.qtyinput').live('keypress',function(e){
//		var code = (e.keyCode ? e.keyCode : e.which);
//
//		if( isUnsignedInteger(String.fromCharCode(code)) || code == 37 || code == 39 || code == 8){
//			return true;
//		}
//
//		return false;
//
//	}).live('keyup',function(e){
//		var code = (e.keyCode ? e.keyCode : e.which);
//
//		if( isUnsignedInteger(String.fromCharCode(code))  || code == 8){
//			jQuery(this).trigger('change');
//		}
//
//	});

	// CHANGED event handler
	jQuery('.qtyinput').live('change',function(e){ // send request, replace HTML content
		var form = jQuery(this).parents('form');
		if (this.value != this.lastValue) {
			var q = this.value;

			//set loader

			// Stop previous ajax-request
			if (this.timer) {
				clearTimeout(this.timer);
			}
			// Start a new ajax-request in X ms
			this.timer = setTimeout(function () {
				form.trigger('changed');
			}, 1000);

			this.lastValue = this.value;
		}
		
	});

	jQuery('#basket_list_form').live('changed', function(){
		var form = jQuery(this);
		var link = form.attr('action');
		jQuery.post(link, form.serialize() ,function(data){
			jQuery('#basketList').html(data);
		});
	});


	// helper function
	function isUnsignedInteger(s) {
		  return (s.toString().search(/^[0-9]+jQuery/) == 0);
	}
});    